package com.dw.controller;

import com.alibaba.fastjson.JSON;
import com.dw.entity.Department;
import com.dw.entity.User;
import com.dw.service.impl.DepServiceImpl;
import com.dw.service.impl.UserServiceImpl;
import com.dw.util.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 部门控制器
 * @Author dw
 * @Date 2019/12/31 16:26
 * @Version 1.0
 **/
@Controller
@RequestMapping("/dep")
public class DepController {
    /**
     用于存放返回到前端的数据
     */
    private Map<String,Object> resultMap;
    /**
     数据列名集合
     */
    private List<String> columnList;


    /**
     实体属性集合
     */
    private List<String> propertyList;

    /**
     条件集合
     */
    private Map<String,String> paramMap;

    /**
        部门服务
     */
    @Autowired
    private DepServiceImpl depService;

    /**
     * 用户服务层接口
     **/
    @Autowired
    private UserServiceImpl userService;

    /**
        加载所有部门信息
     */
    @RequestMapping("/loadAll")
    @ResponseBody
    public Object loadAll(){
        paramMap = new HashMap<>();
        resultMap = new HashMap<>();
        List<Department> depList = depService.findAll("department",paramMap);
        resultMap.put("depList",depList);
        System.out.println(depList);
        return JSON.toJSON(depList);
    }

    /**
     * 跳转到部门列表
     *
     * @param
     * @return
     */
    @RequestMapping("/goDepList")
    public String goDepList(){
        return "dep_list";
    }

    /**
     * 加载所有部门信息
     *
     *
     *
     * @param depId
     * @param limit
     *
     * @param page
     * @return
     */
    @RequestMapping("/loadDepList")
    @ResponseBody
    public Object loadUserList(String depId, Integer page, Integer limit){
        paramMap = new HashMap<>();
        paramMap.put("depId",depId);
        List<Map<Object,Object>> objects = depService.getPageList("department",(page-1)*limit,limit,paramMap);
        for (int i = 0; i < objects.size(); i++) {
            try {
                //查询部门领导
                Map<String,String> userParamMap = new HashMap<>();
                userParamMap.put("columnOne","userId");
                userParamMap.put("propertyOne",objects.get(i).get("depleader").toString());
                User user = userService.findByOne("user",userParamMap);
                //添加college,并且将数据返回到前端页面
                objects.get(i).put("user",user);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        Integer count = userService.getTotalCount("department");
        Map<String,Object> res = new HashMap<>();
        res.put("code",0);
        res.put("msg","");
        res.put("count",count);
        res.put("data",objects);
        return JSON.toJSON(res);
    }


    /**
     * 跳转到修改页面
     *
     * @param
     * @return java.lang.String
     */
    @RequestMapping("/goUpdateDep")
    public String goUpdateUser(){
        return "updateDep";
    }

    /**
     * 跳转到添加界面
     *
     * @param
     * @return java.lang.String
     */
    @RequestMapping("/goAddDep")
    public String goAddHouse(){
        return "addDep";
    }

    /**
     * 部门信息更新
     *
     * @param
     * @return java.lang.Object
     */
    @RequestMapping("/updateDep")
    @ResponseBody
    public Object updateHouse(Department dep){
        List<String> columnList = new ArrayList<>();
        List<String> propertyList = new ArrayList<>();
        Map<String,String> idMap = new HashMap<>();
        resultMap = new HashMap<>();

        columnList.add("depName");
        columnList.add("depleader");

        propertyList.add(dep.getDepName());
        propertyList.add(dep.getDepleader().toString());


        idMap.put("column","depId");
        idMap.put("property",dep.getDepId().toString());

        if(depService.update("department",columnList,propertyList,idMap) > 0){
            resultMap.put("msg","ok");
        }
        else{
            resultMap.put("msg","error");
        }
        return JSON.toJSON(resultMap);
    }

    /**
     * 添加新部门
     *
     * @param dep
     * @return java.lang.Object
     */
    @RequestMapping("/addDep")
    @ResponseBody
    public Object addHouse(Department dep){
        columnList = new ArrayList<>();
        propertyList = new ArrayList<>();
        resultMap = new HashMap<>();

        columnList.add("depName");
        columnList.add("depleader");

        propertyList.add(dep.getDepName());
        propertyList.add(dep.getDepleader().toString());

        if(depService.insert("department",columnList,propertyList) > 0){
            resultMap.put("msg","ok");
        }else{
            resultMap.put("msg","error");
        }
        return JSON.toJSON(resultMap);
    }


    /**
     * 删除部门
     *
     * @param depId
     * @return java.lang.Object
     */
    @RequestMapping("/deleteDep")
    @ResponseBody
    public Object delete(String depId){
        Map<String,String> idMap = new HashMap<>();
        resultMap = new HashMap<>();
        idMap.put("column","depId");
        idMap.put("property",depId.replace("'",""));
        if(depService.delete(idMap) > 0){
            resultMap.put("msg","ok");
        }else{
            resultMap.put("msg","error");
        }
        return JSON.toJSON(resultMap);
    }

}
